package org.baicaixiaozhan.springbootwebdemo1.service.impl;

import lombok.AllArgsConstructor;
import org.baicaixiaozhan.springbootwebdemo1.dao.UserDao;
import org.baicaixiaozhan.springbootwebdemo1.domain.User;
import org.baicaixiaozhan.springbootwebdemo1.service.UserService;
import org.baicaixiaozhan.springbootwebdemo1.vo.UsernameAndPasswordAuthenticationRequest;
import org.springframework.stereotype.Service;

import javax.security.auth.message.AuthException;
import java.util.Objects;

/**
 * @author baicaixiaozhan
 * @since 2021/1/29
 */
@Service
@AllArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserDao userDao;

    /**
     * 对登录 username-password 进行认证处理
     *
     * @param request
     * @return
     * @throws AuthException
     */
    @Override
    public User authenticate(UsernameAndPasswordAuthenticationRequest request) throws AuthException {
        final User user = userDao.findUserByUsername(request.getUsername());
        if (Objects.equals(user.getPassword(),request.getPassword())) {
            return user;
        }
        throw new AuthException("密码错误, 认证失败.");
    }
}
